package com.pair.micro.fee.modules.charge.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pair.micro.common.mybatis.service.impl.BaseServiceImpl;
import com.pair.micro.common.utils.DefinitionCode;
import com.pair.micro.common.utils.PageResult;
import com.pair.micro.fee.modules.charge.convert.ChargeConfigConvert;
import com.pair.micro.fee.modules.charge.convert.GardenGridConvert;
import com.pair.micro.fee.modules.charge.dao.ChargeConfigDao;
import com.pair.micro.fee.modules.charge.entity.ChargeConfigEntity;
import com.pair.micro.fee.modules.charge.entity.GardenGridEntity;
import com.pair.micro.fee.modules.charge.query.ChargeConfigQuery;
import com.pair.micro.fee.modules.charge.service.ChargeConfigService;
import com.pair.micro.fee.modules.charge.vo.ChargeConfigVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@AllArgsConstructor
public class ChargeConfigServiceImpl extends BaseServiceImpl<ChargeConfigDao, ChargeConfigEntity> implements ChargeConfigService {
    @Resource
    private DefinitionCode definitionCode;
    @Override
    public PageResult<ChargeConfigVO> page(ChargeConfigQuery query) {
        IPage<ChargeConfigEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(ChargeConfigConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<ChargeConfigEntity> getWrapper(ChargeConfigQuery query) {
        LambdaQueryWrapper<ChargeConfigEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(query.getGardenId() != null, ChargeConfigEntity::getGardenId, query.getGardenId());
        wrapper.eq(query.getChargeType() != null, ChargeConfigEntity::getChargeType, query.getChargeType());
        wrapper.like(StrUtil.isNotBlank(query.getName()), ChargeConfigEntity::getName, query.getName());
        wrapper.orderByDesc(ChargeConfigEntity::getCreateTime);
        return wrapper;
    }



//    @Override
//    public List<Map<String, Object>> selectFeeTypeCdShow(Map<String, Object> params) {
//        return baseMapper.selectFeeTypeCdShow(params);
//    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(ChargeConfigVO vo) {
        ChargeConfigEntity entity = ChargeConfigConvert.INSTANCE.convert(vo);
        entity.setId(String.valueOf(definitionCode.nextId(DefinitionCode.CODE_PREFIX_CHARGE_CONFIG_ID)));
        baseMapper.insert(entity);
    }

    @Override
    public void update(ChargeConfigVO vo) {
        ChargeConfigEntity entity = ChargeConfigConvert.INSTANCE.convert(vo);
        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    @Override
    public List<ChargeConfigVO> selectChargeConfigList(ChargeConfigQuery query) {
        List<ChargeConfigEntity> entityList =  baseMapper.selectList(getWrapper(query));
        return ChargeConfigConvert.INSTANCE.convertList(entityList);
    }
}
